package com.cennavi.minemapdemo.activity.overlayer;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.View;

import com.cennavi.minemapdemo.activity.BaseActivty;
import com.cennavi.minemapdemo.util.Config;
import com.cennavi.minemapdemo.util.DensityUtil;
import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.util.RandomUtil;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.camera.CameraUpdate;
import com.minedata.minemap.camera.CameraUpdateFactory;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;
import com.minedata.minemap.overlay.Polygon;
import com.minedata.minemap.overlay.PolygonOptions;
import com.minemap.minemapsdk.camera.ImplCameraUpdate;

import java.util.ArrayList;
import java.util.List;

public class PolygonActivity extends BaseActivty {

    //地图视图
    private MapView mapView;
    //地图操作类
    private MineMap mineMap;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        MinemapAccountManager.getInstance(this, Config.MAP_TOK, Config.OFFLINE_TOK, Config.MAP_ID);
        setContentView(R.layout.activity_polygon);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
        //初始化地图
        initMap();
        initView();
    }
    public void zoomIn(View view){
        if (mineMap != null){
            mineMap.moveCamera(CameraUpdateFactory.zoomIn());
        }
    }

    public void zoomOut(View view){
        if (mineMap != null){
            mineMap.moveCamera(CameraUpdateFactory.zoomOut());
        }
    }

    private void initView() {
        findViewById(R.id.zoomin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mineMap != null) {
                    mineMap.animateCamera(CameraUpdateFactory.zoomIn());
                }
            }
        });
        findViewById(R.id.zoomout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mineMap != null) {
                    mineMap.animateCamera(CameraUpdateFactory.zoomOut());
                }
            }
        });
    }

    private void initMap() {

        //mine map sdk 2.0.6之前生效
        // mapView.setStyleUrl(Config.mBaseUrl + Config.MAP_ID);
        mapView.setApiBaseUrl(Config.API_BASE_URL);
        mapView.setApiUrl(Config.API_URL);
        mapView.setSpriteImageUrl(Config.SPRITE_GLYPHS_URL);
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(MineMap oMineMap) {
                mineMap = oMineMap;
//                String style = SDFileConfig.getJsonFile(mContext, "mapstyle.json");
//                mineMap.setStyleJson(style);
                mineMap.getUiSettings().setZoomControlsEnabled(false);
                mineMap.getUiSettings().setRotateGesturesEnabled(false);// 是否可以旋转地图
                mineMap.getUiSettings().setTiltGesturesEnabled(false);
                mineMap.setStyleUrl(Config.BASE_URL + Config.MAP_ID, new MineMap.OnStyleLoadedListener() {

                    @Override
                    public void onStyleLoaded(String s) {

                        //mine map sdk 2.0.6及之后版本这样使用
                        //layers 和 images 都在这里面加载
//                        addLayersToMap();
                    }
                });
                //设置指南针是否显示，默认是显示
                mineMap.getUiSettings().setCompassEnabled(true);
                mineMap.getUiSettings().setCompassGravity(Gravity.LEFT);
//                mineMap.getUiSettings().setCompassImage(getResources().getDrawable(R.mipmap.icon_compass));
                mineMap.getUiSettings().setCompassMargins(DensityUtil.dip2px(mContext, 11), DensityUtil.dip2px(mContext, 80),
                        0, 0);
                mineMap.setCameraPosition(
                        new CameraPosition.Builder().target(new LatLng(Config.mapCentrelat,Config.mapCentrelon)).zoom(14)
                                .tilt(0)//设定倾斜角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .build());
                mineMap.setMaxZoomPreference(17);//设置最高展示级别，最高为17级别
                mineMap.setMinZoomPreference(3);//设置最低的展示级别
                setOnClick();

            }

        });
    }

    private void setOnClick() {
        if (mineMap != null) {
            mineMap.setOnPolygonClickListener(new MineMap.OnPolygonClickListener() {
                @Override
                public void onPolygonClick(@NonNull Polygon polygon) {
                    if (polygon != null) {
                        Snackbar.make(
                                findViewById(android.R.id.content),
                                polygon.getPoints().toString() + "",
                                Snackbar.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private List<Polygon> polygons;

    public void movePolygon(View view) {
        if (polygons != null && polygons.size() > 0)
            for (Polygon polygon : polygons) {
                List<LatLng> lines = new ArrayList<>();
                lines.add(RandomUtil.randomLatLon());
                lines.add(RandomUtil.randomLatLon());
                lines.add(RandomUtil.randomLatLon());
                polygon.setPoints(lines);
            }

    }

    public void addPolygon(View view) {
        LatLng[] lines = new LatLng[]{RandomUtil.randomLatLon(), RandomUtil.randomLatLon(), RandomUtil.randomLatLon()};
        Polygon polygon = mineMap.addPolygon(new PolygonOptions()
                .add(lines)
//                .info("多边形面啊啊啊啊啊啊啊")
                .fillColor(RandomUtil.randomColor())
                .alpha(0.5f)
                .strokeColor(RandomUtil.randomColor()));
        if (polygons == null) {
            polygons = new ArrayList<>();
        }
        polygons.add(polygon);
        mineMap.easeCamera(CameraUpdateFactory.newLatLng(lines[0]), 1000);
    }


    public void removePolygon(View view) {
        if (polygons != null && polygons.size() > 0)
            for (Polygon polygon : polygons) {
                mineMap.removePolygon(polygon);
            }
        polygons.clear();
//            mineMap.clear();
    }

    //生命周期
    @Override
    protected void onStart() {
        super.onStart();
        if (mapView != null)
            mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mapView != null)
            mapView.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mapView != null)
            mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mapView != null)
            mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //2.0.6及之后版本 需要将加入地图的图片资源进行清理
        if (mineMap != null)
            mineMap.removeImagesAll();


        if (mapView != null)
            mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (mapView != null){
            mapView.onLowMemory();
        }
    }
}
